
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[
      {
        label:'视频选择的来源',
        prop:'sourceType',
        type:'picker',
        labelOptions:['从相册选择视频[album]','使用相机拍摄视频[camera]', '从相册选择视频或相机拍摄视频[album, camera]'],
        valueOptions:['["album"]', '["camera"]', '["album", "camera"]'],
        value: '["album"]'
      },
      {
        label:'是否压缩所选择的视频文件',
        prop:'compressed',
        type:'picker',
        labelOptions:['true','false'],
        valueOptions:[true, false],
        value: true
      },
      {
        label:'拍摄视频最长拍摄时间，单位秒',
        prop:'maxDuration',
        type:'number',
        value: 60
      },
      {
        label:'默认拉起的是前置或者后置摄像头。部分 Android 手机下由于系统 ROM 不支持无法生效',
        prop:'camera',
        type:'picker',
        labelOptions:['默认拉起后置摄像头','默认拉起前置摄像头'],
        valueOptions:['back', 'front'],
        value: 'back'
      },
    ],
    formData:{
      sourceType: '["album", "camera"]',
      compressed: true,
      maxDuration: 60,
      camera: 'back'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: '',
    src:''
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
    const formData = {}
    this.data.schema.forEach(item => {
      if(item.type === 'number') {
        if(!isNaN(+e.detail.formData[item.prop]) && e.detail.formData[item.prop] !== '') {
          formData[item.prop] = +e.detail.formData[item.prop]
        }
      } else {
        formData[item.prop] = e.detail.formData[item.prop]
      }
    })
    this.setData({
      formData: formData
    });
    console.log('formData', formData);
  },
  choose1(e) {
    const that = this;
    wx.chooseVideo({
      ...that.data.formData,
      sourceType: JSON.parse(that.data.formData.sourceType || '[]'),
      success(res) {
        that.setData({
          result: res,
          src:res.tempFilePath,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  onUnload() {
  }
})